package com.peng.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.peng.common.R;
import com.peng.dto.DishDto;
import com.peng.entity.Category;
import com.peng.entity.Dish;
import com.peng.entity.DishFlavor;
import com.peng.entity.Setmeal;
import com.peng.handler.CustomException;
import com.peng.service.CategoryService;
import com.peng.service.DishFlavorService;
import com.peng.service.DishService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/dish")
public class DishController {
    @Autowired
    private DishService dishService;
    @Autowired
    private DishFlavorService dishFlavorService;
    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private CategoryService categoryService;
    @GetMapping("/page")
    public R<Page<DishDto>> pageR(Integer page, Integer pageSize, String name){
        Page<DishDto> page1=dishService.pageR(page,pageSize,name);
        return R.success(page1);
    }

    @PostMapping
    public R<String> save(@RequestBody DishDto dishDto){
        Long categoryId = dishDto.getCategoryId();
        String str="dish_"+categoryId;
        redisTemplate.delete(str);
        dishService.saveWithFlavor(dishDto);
        return R.success("添加成功");
    }

    @GetMapping("/{id}")
    public R<DishDto> getID(@PathVariable("id")Long id ){
        Dish byId = dishService.getById(id);
        List<DishFlavor> list= dishFlavorService.list(new LambdaQueryWrapper<DishFlavor>().eq(DishFlavor::getDishId,id));
        DishDto dishDto = new DishDto();
        BeanUtils.copyProperties(byId,dishDto);
        dishDto.setFlavors(list);
        return R.success(dishDto);
    }

    @PutMapping
    public R<String> update(@RequestBody DishDto dishDto){
        Long categoryId = dishDto.getCategoryId();
        String str="dish_"+categoryId;
        redisTemplate.delete(str);
        dishService.updateWithFlavor(dishDto);
        return R.success("修改成功");
    }

    @PostMapping("/status/{sta}")
    public R<String> statusIds(@PathVariable("sta") Integer sta ,@RequestParam("ids") List<Long> ids){
        for (Long id : ids) {
            Dish dish = new Dish();
            dish.setId(id);
            dish.setStatus(sta);
            dishService.updateById(dish);
        }
        return R.success("修改成功");
    }

    @DeleteMapping
    public R<String> deleteDish(@RequestParam("ids") List<Long> ids){
        LambdaQueryWrapper<Dish> setmealLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealLambdaQueryWrapper.in(Dish::getId,ids);
        setmealLambdaQueryWrapper.eq(Dish::getStatus,1);
        if(dishService.count(setmealLambdaQueryWrapper)>0)
            throw new CustomException("存在套餐正在售卖中,不能删除");
        dishService.removeByIds(ids);
        for (Long id : ids) {
            LambdaQueryWrapper<DishFlavor> dishFlavorLambdaQueryWrapper = new LambdaQueryWrapper<>();
            dishFlavorLambdaQueryWrapper.eq(DishFlavor::getDishId,id);
            dishFlavorService.remove(dishFlavorLambdaQueryWrapper);
            Dish byId = dishService.getById(new LambdaQueryWrapper<Dish>().eq(Dish::getId, id));
            String str="dish_"+byId.getCategoryId();
            redisTemplate.delete(str);
        }
        return R.success("删除成功");
    }

    @GetMapping("/list")
    public R<List<DishDto>> getCategory(Long categoryId){
        ArrayList<DishDto> dishDtos=null;
        String str="dish_"+categoryId;
        dishDtos= (ArrayList<DishDto>) redisTemplate.opsForValue().get(str);
        if (dishDtos!=null ){
            return R.success(dishDtos);
        }
        LambdaQueryWrapper<Dish> dishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        dishLambdaQueryWrapper.eq(Dish::getCategoryId,categoryId);
        dishLambdaQueryWrapper.eq(Dish::getStatus,1);
        dishLambdaQueryWrapper.orderByDesc(Dish::getUpdateTime);
        List<Dish> list = dishService.list(dishLambdaQueryWrapper);
        dishDtos = new ArrayList<>();
        for (Dish dish : list) {
            DishDto dishDto = new DishDto();
            BeanUtils.copyProperties(dish,dishDto);
            Long categoryId1 = dish.getCategoryId();
            Category byId = categoryService.getById(categoryId1);
            dishDto.setCategoryName(byId.getName());
            Long id = dish.getId();
            List<DishFlavor> list1 = dishFlavorService.list(new LambdaQueryWrapper<DishFlavor>().eq(DishFlavor::getDishId, id));
            dishDto.setFlavors(list1);
            dishDtos.add(dishDto)   ;
        }
        redisTemplate.opsForValue().set(str,dishDtos,30, TimeUnit.MINUTES);
        return R.success(dishDtos);
    }
}
